<?php

//大图 小图 位置 透明度  类型 文件名是否随机 路径

//打开大图
//打开小图

//获取图片的信息

//算位置


//合并图片

//处理文件名的问题 是否随机

//用什么方式保存图片 imagepng  

//销毁资源

function water($source , $water , $position = 8 , $alpha = 100 , $type = 'jpeg' , $path = './xuxu' , $is_randName = true) 
{
	//打开大图片
	$sourceRes = open($source);
	//打开小图片
	$waterRes = open($water);
	
	//获取大图位置
	$sourceInfo = getimagesize($source);
	
	var_dump($sourceInfo);
	//获取小图的位置
	$waterInfo = getimagesize($water);
	var_dump($waterInfo);
	//算位置 
	switch ($position) {
		case 1:
			$x = 0;
			$y = 0;
			break;
		case 2:
			$x = ($sourceInfo[0] - $waterInfo[0]) / 2;
			$y = 0;
			break;
		case 3:
			$x = $sourceInfo[0] - $waterInfo[0];
			$y = 0;
			break;
		case 4:
			$x = 0;
			$y = ($sourceInfo[1] - $waterInfo[1]) / 2;
			break;
		case 5:
			$x = ($sourceInfo[0] - $waterInfo[0]) / 2;
			$y = ($sourceInfo[1] - $waterInfo[1]) / 2;
			break;
		case 6:
			$x = $sourceInfo[0] - $waterInfo[0];
			$y = ($sourceInfo[1] - $waterInfo[1]) / 2;
			break;
		case 7:
			$x = 0;
			$y = $sourceInfo[1] - $waterInfo[1];
			break;
		case 8:
			$x = ($sourceInfo[0] - $waterInfo[0]) / 2;
			$y = $sourceInfo[1] - $waterInfo[1];
			break;
		case 9:
			$x = $sourceInfo[0] - $waterInfo[0];
			$y = $sourceInfo[1] - $waterInfo[1];
			break;
		default:
			$x = mt_rand(0 , $sourceInfo[0] - $waterInfo[0]);
			$y = mt_rand(0 , $sourceInfo[1] - $waterInfo[1]);
			break;
	}
	//echo $x.'----'.$y;
	
	//把x 轴 y轴的值拿到 给 合并的时候使用
	imagecopymerge($sourceRes , $waterRes , $x , $y , 0 , 0 , $waterInfo[0] , $waterInfo[1] , $alpha);
	
	//使用什么函数输出
	$func = 'image'.$type;
	
	
	//是否启用随机文件名
	
	if ($is_randName) {
		$name = uniqid() . '.' . $type;
	} else {
		$pathInfo = pathinfo($source);
		
		//var_dump($pathInfo);
		$name = $pathInfo['filename'] . '.' . $type;
	}
	
	$path = rtrim($path , '/') . '/' . $name;
	
	//echo $path;
	$func($sourceRes , $path);
	
	imagedestroy($sourceRes);
	imagedestroy($waterRes);
	
	//返回路径地址
	return $path;
}

//打开图片的函数

function open($path)
{
	//var_dump($path);
	//判断这个文件是否存在
	if (!file_exists($path)) {
		return false;
	}
	//获取图片的mime类型
	
	$info = getimagesize($path);
	
	
	
	//var_dump($info);
	
	switch ($info['mime']) {
		case 'image/jpeg':
		case 'image/jpg':
		case 'image/pjpeg':
			$res = imagecreatefromjpeg($path);
			break;
		case 'image/png':
			$res = imagecreatefrompng($path);
			break;
		case 'image/gif':
			$res = imagecreatefromgif($path);
			break;
		case 'image/bmp':
		case 'image/wbmp':
			$res = imagecreatefromwbmp($path);
			break;
	}
	return $res;
}




























